import json

import jsonpath

from Base.BaseSetting import url_path
from Base.PublicFunction import Requst


class WoniuBoss():
    def __init__(self):
        self.url = url_path + '/WoniuBoss4.0'
        self.session = Requst.get_session()

    def search_user(self,empName):
        url = self.url + '/user/queryUser'
        header = {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
        body = 'pageSize=10&pageIndex=1&userName=&empName={}'.format(empName)
        resobj_session = self.session.post(url=url, headers=header, data=body.encode('utf-8'))
        result = resobj_session.text
        result1 = json.loads(result)
        # res = jsonpath.jsonpath(result1,'$..employee_name')
        res = result1["list"][0]["employee_name"]
        # res1 = result1["list"][0]["name"]
        # status_code = resobj_session.status_code
        # content = resobj_session.content
        return res



    def add_role(self,name,des):
        url = self.url + '/role/saveAddRole'
        header = {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
        body = 'role.name={}&role.des={}'.format(name,des)
        resobj_session = self.session.post(url=url, headers=header, data=body.encode('utf-8'))
        content = resobj_session.text
        return content

    def search_role(self,roleName):
        url = self.url + '/role/queryRole'
        header = {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
        body = 'pageSize=10&pageIndex=1&roleName={}'.format(roleName)
        resobj_session = self.session.post(url=url, headers=header, data=body.encode('utf-8'))
        result = resobj_session.text
        result1 = json.loads(result)
        res = result1["list"][0]["name"]
        status_code = resobj_session.status_code
        content = resobj_session.content
        return res,status_code,content

    def alter_role(self,id,name,des):
        url = self.url + '/role/saveEditRole'
        header = {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
        body = 'role.id={}&role.name={}&role.des={}'.format(id,name,des)
        resobj_session = self.session.post(url=url, headers=header, data=body.encode('utf-8'))
        content = resobj_session.text
        return content

    def search_stu(self,stuName):
        url =self.url + '/studentInfo/queryStuByInfo'
        header = {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
        body = 'pageSize=10&pageIndex=1&stuName={}&stuClass=&orientation=&stuStatus=&regionId='.format(stuName)
        resobj_session = self.session.post(url=url, headers=header, data=body.encode('utf-8'))
        result = resobj_session.text
        result1 = json.loads(result)
        res = result1["list"][0]["student_name"]
        # status_code = resobj_session.status_code
        # content = resobj_session.content
        return res

if __name__ == '__main__':
    run = WoniuBoss()
    run.search_user('刘菲菲')
    # run.add_role('老板','')
    # run.search_role('老板')
    # run.alter_role('69','老板','我是老板')
    # run.search_stu('彭振宇')